package com.example.common.utils;

import android.content.Context;

import com.example.common.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author skygge
 * @Date on 2020-02-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：时间格式转换工具类
 */
public class DateUtil {
	
	private static final TimeZone tz = TimeZone.getTimeZone("GMT+8:00");

	private static final SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy-MM-dd");

	private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

	private static final String PATTERN_TIME = "HH:mm";

	private static final String PATTERN_DAY = "yyyy/MM/dd";

	/**
	 * 获取当前当天日期的毫秒数 2012-03-21的毫秒数
	 *
	 * @return
	 */
	public static long getCurrentDayTime() {
		Date d = new Date(System.currentTimeMillis());
		String formatDate = yearFormat.format(d);
		try {
			return (yearFormat.parse(formatDate)).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 得到当前时间
	 * @return 时间
	 */
	public static String getTime(){
		SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 时间戳转换成字符窜
	 * @param milSecond
	 * @return
	 */
	public static String getDateToString(long milSecond) {
		Date date = new Date(milSecond);
		SimpleDateFormat format = new SimpleDateFormat(PATTERN);
		return format.format(date);
	}

	/**
	 * 时间戳转换成字符窜
	 * @param milSecond
	 * @return
	 */
	public static String getDateToString2(long milSecond) {
		Date date = new Date(milSecond);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}

	/**
	 * 将字符串转为时间戳
	 * @param dateString
	 * @return
	 */
	public static long getStringToDate(String dateString) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN);

		Date date = new Date();
		try{
			date = dateFormat.parse(dateString);
		} catch(ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date.getTime();
	}

	/**
	 * 得到7天后的时间
	 * @return 时间
	 */
	public static String getAfter7DayTime(){
		SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND)+604800);
		String time = dateFormat.format(calendar.getTime());
		return time;
	}

    public static long getDateMills(int year, int month, int day) {
        //Date d = new Date(year, month, day);
		// 1960 4 22
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(year, month, day);
		TimeZone tz = TimeZone.getDefault();
		calendar.setTimeZone(tz);
        return calendar.getTimeInMillis();
    }

	/**
	 * 根据毫秒时间戳来格式化字符串
	 * 今天显示今天、昨天显示昨天、前天显示前天.
	 * 早于前天的显示具体年-月-日，如2017-06-12；
	 * @param timeStamp 毫秒值
	 * @return 今天 昨天 前天 或者 yyyy-MM-dd HH:mm:ss类型字符串
	 */
	public static String format(Context context, long timeStamp) {
		long curTimeMillis = System.currentTimeMillis();
		Date curDate = new Date(curTimeMillis);
		int todayHoursSeconds = curDate.getHours() * 60 * 60;
		int todayMinutesSeconds = curDate.getMinutes() * 60;
		int todaySeconds = curDate.getSeconds();
		int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
		long todayStartMillis = curTimeMillis - todayMillis;
		if(timeStamp >= todayStartMillis) {
			SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_TIME);
			return  sdf.format(new Date(timeStamp));
		}
		int oneDayMillis = 24 * 60 * 60 * 1000;
		int sevenDayMillis = 24 * 60 * 60 * 1000 * 6;
		long yesterdayStartMilis = todayStartMillis - oneDayMillis;
		if(timeStamp >= yesterdayStartMilis) {
			return context.getString(R.string.yesterday);
		}
		long yesterdayBeforeStartMilis = yesterdayStartMilis - sevenDayMillis;
		if(timeStamp >= yesterdayBeforeStartMilis) {
			return getWeek(context, timeStamp);
		}

		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_DAY);
		return  sdf.format(new Date(timeStamp));
	}

	public static String getWeek(Context context, long time) {
		Calendar cd = Calendar.getInstance();
		cd.setTime(new Date(time));
		int week  = cd.get(Calendar.DAY_OF_WEEK); //获取星期
		return context.getResources().getStringArray(R.array.week_day)[week-1];
	}

	/**
	 * 判断两个时间戳是否在同一天
	 * @param millis1 时间戳1
	 * @param millis2 时间戳2
	 * @return 布偶
	 */
	public static boolean isSameDay(long millis1, long millis2) {
		long interval = millis1 - millis2;
		return interval < 86400000 && interval > -86400000 && millis2Days(millis1, TimeZone.getDefault()) == millis2Days(millis2, TimeZone.getDefault());
	}

	private static long millis2Days(long millis, TimeZone timeZone) {
		return (((long) timeZone.getOffset(millis)) + millis) / 86400000;
	}

	public static long getCurrentTime() {
		return System.currentTimeMillis() / 1000;
	}

	/**
	 * 将时间戳转为代表"距现在多久之前"的字符串
	 * @param timestr   时间戳
	 * @return
	 */
	public static String getStandardDate(String timestr) {

	    StringBuffer sb = new StringBuffer();

        long t = Long.parseLong(timestr);
	    long time = System.currentTimeMillis() - (t*1000);
	    long mill = (long) Math.ceil(time /1000);//秒前

	    long minute = (long) Math.ceil(time/60/1000.0f);// 分钟前

	    long hour = (long) Math.ceil(time/60/60/1000.0f);// 小时

	    long day = (long) Math.ceil(time/24/60/60/1000.0f);// 天前

	    if (day - 1 > 0) {
	        sb.append(day + "天");
	    } else if (hour - 1 > 0) {
	        if (hour >= 24) {
	            sb.append("1天");
	        } else {
	            sb.append(hour + "小时");
	        }
	    } else if (minute - 1 > 0) {
	        if (minute == 60) {
	            sb.append("1小时");
	        } else {
	            sb.append(minute + "分钟");
	        }
	    } else if (mill - 1 > 0) {
	        if (mill == 60) {
	            sb.append("1分钟");
	        } else {
	            sb.append(mill + "秒");
	        }
	    } else {
	        sb.append("刚刚");
	    }
	    return sb.toString();
	}


	/**
	 * 通过年份和月份 得到当月的日子
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getMonthDays(int year, int month) {
		month++;
		switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				return 31;
			case 4:
			case 6:
			case 9:
			case 11:
				return 30;
			case 2:
				if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
					return 29;
				}else{
					return 28;
				}
			default:
				return  -1;
		}
	}
	/**
	 * 返回当前月份1号位于周几
	 * @param year
	 * 		年份
	 * @param month
	 * 		月份，传入系统获取的，不需要正常的
	 * @return
	 * 	日：1		一：2		二：3		三：4		四：5		五：6		六：7
	 */
	public static int getFirstDayWeek(int year, int month){
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, 1);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public static int getSecondInDay(String time){
		try {
			if(time.indexOf(":")==2){
				int hour = Integer.parseInt(time.substring(0, 2));
				int min  = Integer.parseInt(time.substring(3, 5));
                int second = Integer.parseInt(time.substring(6));
				return 3600 * hour + 60 * min + second;
			}else{
				return  -1;
			}
		}catch (Exception e){
			e.printStackTrace();
			return -1;
		}
	}
}
